package com.example.ui;

import com.example.model.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserHystrixService {
    @Autowired
    private UserService userService;

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallbackUser")
    public User feignUser(Integer age) {
        return userService.user(age);
    }


    @HystrixCommand(fallbackMethod = "fallbackUser")
    public User ribbonUser(Integer age) {
        Map<String, Object> map = new HashMap<>();
        map.put("age", age);
        ResponseEntity<User> responseEntity = restTemplate.getForEntity("http://localhost:8080/user/user?age={age}", User.class, map);
        return responseEntity.getBody();
    }

    public User fallbackUser(Integer age) {
        User user = new User();
        user.setUserName("fallback");
        user.setAge(age);
        return user;
    }
}
